using System;
using System.Collections;
using System.Collections.Generic;
 
using UnityEngine;
namespace Topifish.Ads {
    /// <summary>
    /// 广告代理
    /// <para>这是一个适配多个广告SDK的接口</para>
    /// </summary>
    public class AdAgent {
        static AdAgent instance;
        public static AdAgent GetInstance {
            get {
                if (instance == null) {
                    instance = new AdAgent();
                }
                return instance;
            }
        }
        I_ADInterface adInterface;
        MonoBehaviour mono;
        public void Init() {
            // 
#if AD_Admob44
            adInterface = new AdmobInterface();
#elif AD_Yomob
            adInterface = new YomobInterface();
#elif AD_LronSource
            adInterface = new IronSourceInterface();
#else
            adInterface = new YomobInterface();
#endif
            if (adInterface == null) {
                Debug.Log("AdAgent_无广告___");
                return;
            }
            mono = adInterface.Init();
            mono.StartCoroutine(I_Update());


            adInterface.d_RewardVideoResultCallBack =delegate(E_AdCallbackType callback) {
                d_RewardVideoResultCallBack(adLogicName,callback);
            } ;
            adInterface.d_InterstitialResultCallBack = delegate (E_AdCallbackType callback) {
                d_InterstitialResultCallBack(callback);
            };

            d_RewardVideoResultCallBack += delegate (string adLogicType,E_AdCallbackType callbackType) {
                Debug.LogWarningFormat("AdAgent_收到奖励视频广告_________________________{0}_________{1}______",((int)callbackType), callbackType);
            };
            d_InterstitialResultCallBack += delegate (E_AdCallbackType callbackType) {
                Debug.LogWarningFormat("AdAgent_收到插页广告_________________________{0}_________{1}______", ((int)callbackType), callbackType);
            };
        }
        public void StartLoadAds() {
            if (mono != null) {
                mono.StartCoroutine(I_StartLoadAds());
            }
        }
        IEnumerator I_StartLoadAds() {
            while (adInterface.IsInitComplete==false) {
                yield return new WaitForSeconds(0.5f);
            }
            adInterface.StartLoadAds();
        }
        /// <summary>
        /// 奖励视频结果回调
        /// </summary>
        public event Action<string, E_AdCallbackType> d_RewardVideoResultCallBack;
        /// <summary>
        /// 插页广告结果回调
        /// </summary>
        public event Action<E_AdCallbackType> d_InterstitialResultCallBack;
        IEnumerator I_Update() {
            if (adInterface == null) {
                //Debug.Log("无广告___");
                yield break;
            }
            try {

            } catch (Exception ex){
                Debug.LogWarningFormat("AdAgent出错__{0}__{1}", ex.Message, ex.StackTrace);
            }
            yield return new WaitForSeconds(0);
            mono.StartCoroutine(I_Update());
        }

        /// <summary>
        /// 当前设备是否支持google广告
        /// </summary>
        public bool CurrentDeviceIsSupport() {
            if (adInterface == null) {
                Debug.Log("无广告___");
                return false;
            }
            return adInterface.CurrentDeviceIsSupport();
        }
        /// <summary>
        /// 是否初始化完成
        /// </summary>
        public bool IsInitComplete {
            get {
                if (adInterface == null) {
                    //Debug.Log("无广告___");
                    return false;
                }
                return adInterface.IsInitComplete;
            }
        }
        /// <summary>
        /// 间质全屏广告是否准备完成
        /// </summary>
        public bool IsInterstitialReady() {
            if (adInterface == null) {
                //Debug.Log("无广告___");
                return false;
            }
            return adInterface.IsInterstitialReady();
        }
        /// <summary>
        /// 间质全屏广告开启
        /// </summary>
        public void ShowInterstitial(string adLogicName) {
            if (adInterface == null) {
                Debug.Log("无广告___");
                return;
            }
            this.adLogicName = adLogicName;
            adInterface.ShowInterstitial();
        }

        /// <summary>
        /// 视频广告是否准备完成
        /// </summary>
        public bool IsRewardedVideoReady() {
            if (adInterface == null) {
               // Debug.Log("无广告___");
                return false;
            }
            return adInterface.IsRewardedVideoReady();
        }
        /// <summary>
        /// 离线广告是否加载完成
        /// </summary>
        public bool IsOffLineRewardedVideoReady() {
            if (adInterface == null) {
               // Debug.Log("无广告___");
                return false;
            }
            return adInterface.IsOffLineRewardedVideoReady();
        }

        public bool IsSkillUpgradingVideoReady() {
            if (adInterface == null) {
                // Debug.Log("无广告___");
                return false;
            }
            return adInterface.IsSkillUpgradingVideoReady();
        }
        /// <summary>
        /// 显示奖赏视频---激励-双倍单局收益
        /// </summary>
        public void ShowRewardedVideo(string adLogicName) {
            if (adInterface == null) {
                Debug.Log("无广告___");
                return;
            }
            this.adLogicName = adLogicName;
            adInterface.ShowRewardedVideo();
        }
        string adLogicName;
        /// <summary>
        /// 显示离线奖赏视频---
        /// </summary>
        public void ShowOffLineRewardedVideo() {
            if (adInterface == null) {
                Debug.Log("无广告___");
                return;
            }
            adInterface.ShowOffLineRewardedVideo();
        }

        public void ShowSkillUpgradingVideo() {
            if (adInterface == null) {
                Debug.Log("无广告___");
                return;
            }
            adInterface.ShowSkillUpgradingVideo();
        }
    }
    public enum E_AdCallbackType {
        /// <summary>
        /// 加载视频
        /// </summary>
        e01_AdLoaded = 1,
        /// <summary>
        /// 加载失败
        /// </summary>
        e02_onAdLoadFailed = 2,
        /// <summary>
        /// 加载成功
        /// </summary>
        e03_onAdLoadSuccess = 3,
        /// <summary>
        /// 打开视频
        /// </summary>
        e08_onAdOpened = 8,
        /// <summary>
        /// 启动-开始
        /// </summary>
        e10_onAdStarted = 10,
        /// <summary>
        /// 打开失败
        /// </summary>
        e11_onAdOpenFailed = 11,
        /// <summary>
        /// 视频被关闭
        /// </summary>
        e12_onAdClosed = 12,
        /// <summary>
        /// 奖励
        /// </summary>
        e16_onAdRewarded = 16,
        /// <summary>
        /// 视频播放完成
        /// </summary>
        e19_onAdCompleted = 19,
    }
    public enum E_AdType {
        e01_Banner = 1,
        e02_Interstitial = 2,
        e03_RewardVideo = 3,
    }
    internal abstract class I_ADInterface {
        public abstract MonoBehaviour Init();

        /// <summary>
        /// 奖励视频结果回调
        /// </summary>
        public Action<E_AdCallbackType> d_RewardVideoResultCallBack;
        /// <summary>
        /// 插页广告结果回调
        /// </summary>
        public Action<E_AdCallbackType> d_InterstitialResultCallBack;

        public abstract void StartLoadAds();
        /// <summary>
        /// 当前设备是否支持google广告
        /// </summary>
        public abstract bool CurrentDeviceIsSupport();
        /// <summary>
        /// 是否初始化完成
        /// </summary>
        public abstract bool IsInitComplete { get; }
        /// <summary>
        /// 间质全屏广告是否准备完成
        /// </summary>
        public abstract bool IsInterstitialReady();
        /// <summary>
        /// 间质全屏广告开启
        /// </summary>
        public abstract void ShowInterstitial();

        /// <summary>
        /// 视频广告是否准备完成
        /// </summary>
        public abstract bool IsRewardedVideoReady();
        /// <summary>
        /// 离线广告是否加载完成
        /// </summary>
        public abstract bool IsOffLineRewardedVideoReady();

        /// <summary>
        /// 显示奖赏视频---激励-双倍单局收益
        /// </summary>
        public abstract void ShowRewardedVideo();

        /// <summary>
        /// 显示离线奖赏视频---
        /// </summary>
        public abstract void ShowOffLineRewardedVideo();

        public abstract bool IsSkillUpgradingVideoReady();
        public abstract void ShowSkillUpgradingVideo();
    }



    internal class AdmobInterface : I_ADInterface {
       // AdmobAgent admobAgent;

        public override MonoBehaviour Init() {
            //admobAgent = AdmobAgent.GetInstance;
            //new CheckDeviceIsSupport().InitCheck();
            //admobAgent.d_InterstitialResultCallBack += delegate (AdmobAgent.E_AdCallbackType callbackType) {
            //    d_InterstitialResultCallBack((E_AdCallbackType)callbackType);
            //};
            //admobAgent.d_RewardVideoResultCallBack += delegate (AdmobAgent.E_AdCallbackType callbackType) {
            //    d_RewardVideoResultCallBack((E_AdCallbackType)callbackType);
            //};
            //return admobAgent;
            return null;
        }

        public override void StartLoadAds() {
          //  admobAgent.StartLoadAds();
        }

        public override bool IsInitComplete {
            get {
                return false;// admobAgent.IsInitComplete; 
            } }

        public override bool CurrentDeviceIsSupport() {
            return false;// admobAgent.CurrentDeviceIsSupport();
        }

        public override bool IsInterstitialReady() {
            return false;// admobAgent.IsInterstitialReady();
        }

        public override bool IsOffLineRewardedVideoReady() {
            return false;//admobAgent.IsOffLineRewardedVideoReady();
        }

        public override bool IsRewardedVideoReady() {
            return false;//admobAgent.IsRewardedVideoReady();
        }

        public override bool IsSkillUpgradingVideoReady() {
            return false;//admobAgent.IsSkillUpgradingVideoReady();
        }

        public override void ShowInterstitial() {
           // admobAgent.ShowInterstitial();
        }

        public override void ShowOffLineRewardedVideo() {
           // admobAgent.ShowOffLineRewardedVideo();
        }

        public override void ShowRewardedVideo() {
           // admobAgent.ShowRewardedVideo();
        }

        public override void ShowSkillUpgradingVideo() {
           // admobAgent.ShowSkillUpgradingVideo();
        }
    }


    internal class YomobInterface : I_ADInterface {
 
        YomobAgent yomobAgent;
        public override MonoBehaviour Init() {
            yomobAgent = YomobAgent.GetInstance;
            yomobAgent.Init();
            yomobAgent.d_InterstitialResultCallBack += delegate (E_AdCallbackType callbackType) {
                d_InterstitialResultCallBack((E_AdCallbackType)callbackType);
            };
            yomobAgent.d_RewardVideoResultCallBack += delegate (E_AdCallbackType callbackType) {
                d_RewardVideoResultCallBack((E_AdCallbackType)callbackType);
            };
            return yomobAgent;
        }
        public override void StartLoadAds() {
            yomobAgent.StartLoadAds();
        }
        public override bool IsInitComplete { get { return yomobAgent.IsInitComplete; } }

        public override bool CurrentDeviceIsSupport() {
            return yomobAgent.CurrentDeviceIsSupport();
        }



        public override bool IsInterstitialReady() {
            return yomobAgent.IsInterstitialReady();
        }

        public override bool IsOffLineRewardedVideoReady() {
            return yomobAgent.IsOffLineRewardedVideoReady();
        }

        public override bool IsRewardedVideoReady() {
            return yomobAgent.IsRewardedVideoReady();
        }

        public override void ShowInterstitial() {
            yomobAgent.ShowInterstitial();
        }

        public override void ShowOffLineRewardedVideo() {
            yomobAgent.ShowOffLineRewardedVideo();
        }

        public override void ShowRewardedVideo() {
            yomobAgent.ShowRewardedVideo();
        }
        public override bool IsSkillUpgradingVideoReady() {
            return yomobAgent.IsOffLineRewardedVideoReady();
        }
        public override void ShowSkillUpgradingVideo() {
            yomobAgent.ShowOffLineRewardedVideo();
        }

 



 
    }

 
}
